package com.ztf.gui;

import com.ztf.bean.User;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.SwingConstants;

/**
 * 好友列表
 */
public class ImageCellRender extends DefaultListCellRenderer {
	private static final long serialVersionUID = 1L;
	static List<String> names = new ArrayList<String>();

	@Override
	public Component getListCellRendererComponent(JList<? extends Object> list,
			Object value, int index, boolean isSelected, boolean cellHasFocus){
		//instanceof 判断其左边对象是否为其右边类的实例
		if(value instanceof User){
			User user = (User) value;
			try{
				//a.这里可以用html来控制字体的颜色
				String text1 = "<html><body color=\"red\">"+user.getName()+"</body></html>";
				//b.也可以通过设置前景色来改变字体颜色
				setForeground(Color.blue);

				setText(text1);
//				//设置背景色
				setBackground(Color.white);

				InputStream is= new ByteArrayInputStream(user.getHead());
				BufferedImage image = ImageIO.read(is);
				setIcon(new ImageIcon(image));
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(isSelected){

			System.out.println("被点击");

		}
		return this;
	}
}